package org.linlinjava.litemall.wx;

import org.junit.Test;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author zhy
 * @date 2019-06-18 15:52
 **/
public class WaitNotify {
	private YaLiTest yaLiTest;

	@Test
	public void time(){
		LocalTime midnight = LocalTime.MIDNIGHT;
		LocalDate today = LocalDate.now();
		LocalDateTime todayMidnight = LocalDateTime.of(today, midnight);
		LocalDateTime tomorrowMidnight = todayMidnight.plusDays(1);
		int expireTime = (int)TimeUnit.NANOSECONDS.toSeconds(Duration.between(LocalDateTime.now(), tomorrowMidnight).toNanos());
		System.out.println(expireTime);
	}
	@Test
	public void tsdfa(){
		Short num = 2;
		System.out.println(Integer.valueOf(num));
	}
	@Test
	public void test4(){
		int[] arr = {3,5,7};
		System.out.println(arr[(int)(Math.random()*arr.length)]);
	}
	@Test
	public void test2(){
		LocalTime midnight = LocalTime.MIDNIGHT;
		LocalDate today = LocalDate.now();
		LocalDateTime todayMidnight = LocalDateTime.of(today, midnight);
		LocalDateTime tomorrowMidnight = todayMidnight.plusDays(1);
		System.out.println(TimeUnit.NANOSECONDS.toHours(Duration.between(LocalDateTime.now(), tomorrowMidnight).toNanos()));
	}
	@Test
	public void test1() throws Exception {
		String obj = ""; //开启一个线程，代表小朋友
		new Thread(() -> {
			if (yaLiTest == null) { //若没有冰激凌
				synchronized (obj) {
					try {
						System.out.println("没有冰激凌，小朋友不开心，等待...");
						obj.wait();
					}
					catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			System.out.println("小朋友买到冰激凌，开心回家");
			try {
				System.out.println(yaLiTest.getClass().getConstructor());
			}
			catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		}).start();
		Thread.sleep(3000L);    // 3秒之后
		yaLiTest = new YaLiTest();      //店员做了一个冰激凌
		synchronized (obj) {     //拿到小朋友加的钥匙，去叫他
			obj.notifyAll();
			System.out.println("通知小朋友");
		}
	}
}
